<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class ZlYearActiveCouponRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_year_active_coupon_record';

    protected $cacheModel = 'active.year_give_coupon';

    /**
     * 获取 活动抢券 数量信息
     * @param int $activeFlag 活动标识
     * @return array
     * @throws \Exception
     */
    public function getActiveCouponGiveInfo(int $activeFlag)
    {
        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag)->getCache();

        return $resData ?: [];
    }

    /**
     * 获取 领取优惠券信息
     * @param int $activeFlag
     * @param int $uid
     * @param int $flag
     * @return array|mixed|object
     * @throws \Exception
     */
    public function getDataDetails(int $activeFlag, int $uid, int $flag)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag . ':' . $flag . ':' . $uid)->getDataAndSet(function () use ($uid, $activeFlag, $flag)
        {
            try
            {
                return $this->where(['active_flag' => $activeFlag, 'uid' => $uid, 'type' => 3, 'flag' => $flag])->findOrFail()->toArray();
            }
            catch (\Exception $e)
            {
                \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                return [];
            }
        });

        return arrayKeyData2int($data, ['id', 'active_flag', 'uid', 'type', 'flag', 'cid', 'create_time']);
    }

    /**
     * 设置 领取优惠券信息
     * @param int $id
     * @param int $activeFlag
     * @param int $uid
     * @param int $flag
     * @param int $cid
     * @return bool
     */
    public function setDataInfo(int $id, int $activeFlag, int $uid, int $flag, int $cid)
    {
        try
        {
            $this->where(['id' => $id])->update(['cid' => $cid]);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag . ':' . $flag . ':' . $uid)->setCache(['cid' => $cid]);
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * 活动抢券
     * @param int $activeFlag
     * @param int $flag
     * @param int $changeNum
     * @return false|int
     * @throws \Exception
     */
    public function ActiveCouponGiveChange(int $activeFlag, int $flag, int $changeNum)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag)->setCacheInc($flag, 'details', $changeNum);
    }

    /**
     * 获取 活动抢券 用户
     * @param int $activeFlag 活动标识
     * @param int $flag 10000 + 131 = 10131 10000=>免费券 20000=>折扣券 + 131月日
     * @return array
     * @throws \Exception
     */
    public function getActiveCouponGiveUids(int $activeFlag, int $flag)
    {
        $cacheModel = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel)->setCacheKeyDetail('give_coupon_uids', $activeFlag . ':' . $flag);
        // 获取领取过优惠券的用户
        return $cacheModel->redisHandler->lRange($cacheModel->cacheKey['give_coupon_uids'], 0, -1);
    }

    /**
     * 添加 活动抢券 用户
     * @param int $activeFlag 活动标识
     * @param int $flag
     * @param int $uid
     * @throws \Exception
     */
    public function addActiveCouponGiveUid(int $activeFlag, int $flag, int $uid)
    {
        $cacheModel = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel)->setCacheKeyDetail('give_coupon_uids', $activeFlag . ':' . $flag);
        // 获取领取过优惠券的用户
        $cacheModel->redisHandler->lPush($cacheModel->cacheKey['give_coupon_uids'], $uid);
    }

    /**
     * 设置用户 是否已经 抢券信息
     * @param int $activeFlag
     * @param int $uid
     * @param int $flag
     * @throws \Exception
     */
    public function setUserCouponGiveInfo(int $activeFlag, int $uid, int $flag)
    {
        CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_give_coupon', $activeFlag . ':' . $uid)
            ->setCache([$flag => 1], 'user_is_give_coupon', false);
    }

    /**
     * 获取用户 是否已经 抢券信息
     * @param int $activeFlag
     * @param int $uid
     * @param int $flag
     * @return bool
     * @throws \Exception
     */
    public function getUserCouponGiveInfo(int $activeFlag, int $uid, int $flag)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_give_coupon', $activeFlag . ':' . $uid)
            ->getCache([$flag], 'user_is_give_coupon');
        return $data ? true : false;
    }

    /**
     * 设置用户 是否已经 兑换优惠券
     * @param int $activeFlag
     * @param int $uid
     * @param int $flag 1=>年运免费券 2=>一生报告免费券
     * @throws \Exception
     */
    public function setUserCouponIsExchangeInfo(int $activeFlag, int $uid, int $flag)
    {
        CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_exchange_coupon', $activeFlag . ':' . $uid)
            ->setCache([$flag => 1], 'user_is_exchange_coupon', false);
    }

    /**
     * 获取用户 是否已经 兑换优惠券
     * @param int $activeFlag
     * @param int $uid
     * @param int $flag 1=>年运免费券 2=>一生报告免费券     2022五一活动【1=>月度日运 2=>免费券选择兑换 3=>光荣小礼包 4=>光荣大礼包】
     * @return bool
     * @throws \Exception
     */
    public function getUserCouponIsExchangeInfo(int $activeFlag, int $uid, int $flag)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_exchange_coupon', $activeFlag . ':' . $uid)
            ->getCache([$flag], 'user_is_exchange_coupon');
        return $data ? true : false;
    }

    /**
     * 获取用户 是否已经 兑换优惠券 信息
     * @param int $activeFlag
     * @param int $uid
     * @param array $flags
     * @return bool
     * @throws \Exception
     */
    public function getUserCouponExchangeInfo(int $activeFlag, int $uid, array $flags = [])
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_exchange_coupon', $activeFlag . ':' . $uid)
            ->getCache($flags, 'user_is_exchange_coupon');

        return $data;
    }
}
